package com.springboot2.infrastructure.websocket;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebMvc
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private WebSocketHandlerServer webSocketHandler;

    @Autowired
    private ConfigHandshakeInterceptor handshakeInterceptor;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {

        /**
         * 添加一个处理器 还有 定义处理器的处理路径
         * webSocketHandler : 处理消息
         * /ws ：websocket的访问地址 ws://path/ws
         */
        webSocketHandlerRegistry.addHandler(webSocketHandler,"/socket").setAllowedOrigins("*").addInterceptors(handshakeInterceptor);

    }
}
